package com.synjones.zeuscloudweb.service.impl;

import com.synjones.zeus.model.authorize.AuthorizeInfo;
import com.synjones.zeus.model.web.LisenceBean;
import com.synjones.zeuscloudweb.service.AuthorizeService;
import com.synjones.zeuscloudweb.util.ccl.datastructure.impl.BaseDto;
import com.synjones.zeuscloudweb.util.ccl.util.G4Utils;
import com.synjones.zeuscloudweb.dao.AuthorizeDaoMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Think 2018/5/25 1:23
 */
@Service
public class AuthorizeServiceImpl implements AuthorizeService {

    private final static Logger logger = LoggerFactory.getLogger(AuthorizeServiceImpl.class);

    @Resource
    private AuthorizeDaoMapper authorizeDaoMapper;

    @Override
    public List<AuthorizeInfo> getAuthorizeListByPage(String startTime, String endTime, String orderColumn, String orderDir, AuthorizeInfo authorizeInfo) {
        return authorizeDaoMapper.getAuthorizeList(startTime, endTime, orderColumn, orderDir, authorizeInfo);
    }

    @Override
    public List<AuthorizeInfo> getLisenceListByPage(String startTime, String endTime, String orderColumn, String orderDir, AuthorizeInfo authorizeInfo) {
        return authorizeDaoMapper.getLisenceList(startTime, endTime, orderColumn, orderDir, authorizeInfo);
    }

    @Override
    public BaseDto deleteAuthorizeByImei(String imei) {
        BaseDto jsonDto = new BaseDto();
        try{
            // 删除
            authorizeDaoMapper.deleteAuthorizeByImei(imei);
            jsonDto.put("success", new Boolean(true));
            jsonDto.put("msg", "操作成功!");
            jsonDto.put("data",null);
        }catch (Exception e){
            jsonDto.put("success", new Boolean(false));
            jsonDto.put("msg", "操作失败!");
            jsonDto.put("data",null);
        }
        return jsonDto;
    }

    @Override
    public BaseDto deleteLisenceById(String id) {
        BaseDto jsonDto = new BaseDto();
        try{
            // 删除
            authorizeDaoMapper.deleteLisenceById(id);
            jsonDto.put("success", new Boolean(true));
            jsonDto.put("msg", "操作成功!");
            jsonDto.put("data",null);
        }catch (Exception e){
            jsonDto.put("success", new Boolean(false));
            jsonDto.put("msg", "操作失败!");
            jsonDto.put("data",null);
        }
        return jsonDto;
    }

    @Override
    public BaseDto insertLisence(LisenceBean lisenceBean) {
        BaseDto jsonDto = new BaseDto();
        String orgCode = lisenceBean.getOrgCode();
        try {
            LisenceBean lisence = authorizeDaoMapper.selectLisenceByOrgCode(orgCode);
            if (G4Utils.isEmpty(lisence)) {
                lisenceBean.setPassWord(G4Utils.encryptBasedDes(lisenceBean.getPassWord()));
                lisenceBean.setAuthenPwd(G4Utils.encryptBasedDes(lisenceBean.getAuthenPwd()));
                int count = authorizeDaoMapper.insertLisence(lisenceBean);
                if (count > 0) {
                    jsonDto.put("success", new Boolean(true));
                    jsonDto.put("msg", "操作成功!");
                }
            } else {
                jsonDto.put("success", new Boolean(false));
                jsonDto.put("msg", "此机构已添加许可!");
            }
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            jsonDto.put("success", new Boolean(false));
            jsonDto.put("msg", "操作失败请联系管理员!");
        }
        return jsonDto;
    }

    @Override
    public LisenceBean selectLisenceById(String id) {
        return authorizeDaoMapper.selectLisenceById(id);
    }

    @Override
    public BaseDto updateLisence(LisenceBean lisenceBean) {
        BaseDto jsonDto = new BaseDto();
        try {
            authorizeDaoMapper.updateLisence(lisenceBean);
            jsonDto.put("orgCode", lisenceBean.getOrgCode());
            jsonDto.put("success", new Boolean(true));
            jsonDto.put("msg", "操作成功!");
            logger.debug("orgCode为" + lisenceBean.getOrgCode() + "执行修改");
        } catch (Exception e) {
            jsonDto.put("orgCode", lisenceBean.getOrgCode());
            jsonDto.put("success", new Boolean(false));
            jsonDto.put("msg", "操作失败请联系管理员!");
        }
        return jsonDto;
    }
}
